package com.example.yls.demoa;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;

public class EditActivity extends AppCompatActivity {
    private EditText edt_name;
    private EditText edt_age;

    private RadioButton radioBtn1;
    private RadioButton radioBtn2;
    private RadioButton radioBtn3;

    private Button btn_confirm_edt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit);
        setTitle("编辑学生");

        final Intent intent = getIntent();
        final int no = intent.getIntExtra("NO",18000);
        String name = intent.getStringExtra("NAME");
        int age = intent.getIntExtra("AGE",0);
        int imgId = intent.getIntExtra("IMG",R.drawable.dog1);

        edt_name = findViewById(R.id.edt_name);
        edt_age = findViewById(R.id.edt_age);

        radioBtn1 = findViewById(R.id.radioBtn1);
        radioBtn2 = findViewById(R.id.radioBtn2);
        radioBtn3 = findViewById(R.id.radioBtn3);

        btn_confirm_edt = findViewById(R.id.btn_confirm_edt);

        edt_name.setText(name);
        edt_age.setText(String.valueOf(age));
        switch (imgId){
            case R.drawable.dog1:
                radioBtn1.setChecked(true);
                break;

            case R.drawable.dog2:
                radioBtn2.setChecked(true);
                break;

            case R.drawable.dog3:
                radioBtn3.setChecked(true);
                break;
        }
        btn_confirm_edt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = edt_name.getText().toString();
                int age = Integer.parseInt(edt_age.getText().toString());
                int imgId = R.drawable.dog1;

                if(radioBtn1.isChecked()){
                    imgId = R.drawable.dog1;
                }else if(radioBtn2.isChecked()){
                    imgId = R.drawable.dog2;
                }else if(radioBtn3.isChecked()){
                    imgId = R.drawable.dog3;
                }

                Intent intent = new Intent();
                intent.putExtra("NAME",name);
                intent.putExtra("NO",no);
                intent.putExtra("AGE",age);
                intent.putExtra("IMG",imgId);
                setResult(3001,intent);

                finish();
            }
        });


    }
}
